﻿using System;
using System.Data;
using TimKen.DataEntity;
using System.Collections.Generic;
using TimKen.DataAccess;

namespace TimKen.BussLogic
{
    /// <summary>
    /// 属性业务逻辑操作类
    /// </summary>
    public class POptionManager : BaseManager
    {
        private readonly TimKen.DataAccess.POptionDAO dal = new TimKen.DataAccess.POptionDAO();
        public POptionManager()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int OptionID, int LanguageID)
        {
            return dal.Exists(OptionID, LanguageID);
        }

        public bool Exists(IList<POption> list)
        {
            long OptionCount = this.GetListCount(" (OptionName='" + list[0].OptionName + "' or OptionName='" + list[1].OptionName + "') and OptionID<>" + list[0].OptionID);
            if (OptionCount == 0)
                return false;
            else
                return true;
        }

        public bool Exists(TimKen.DataEntity.POption model)
        {
            long OptionCount = this.GetListCount(" OptionName='" + model.OptionName + "' and OptionID<>" + model.OptionID);
            if (OptionCount == 0)
                return false;
            else
                return true;
        }


        /// <summary>
        /// 增加一条数据(返回主键),添加失败返回-1
        /// </summary>
        public int Add(IList<POption> list)
        {
            ValidMessage = "";
            bool isExists = Exists(list);
            if (isExists)
            {
                ValidMessage = GetLangString("ExistOption"); ;
                return -1;
            }
            else
            {
                int newid = GetMaxId();
                foreach (POption po in list)
                {
                    po.OptionID = newid;
                    dal.Add(po);
                }
                ValidMessage = GetLangString("SaveSuccessful");
                return newid;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TimKen.DataEntity.POption model)
        {
            if (Exists(model))
            {
                ValidMessage = GetLangString("ExistOption");
                return false;
            }
            else
            {
                dal.Update(model);
                ValidMessage = GetLangString("SaveSuccessful");
                return true;
            }
        }

        public bool Update(IList<POption> list)
        {
            ValidMessage = "";
            if (Exists(list))
            {
                ValidMessage = GetLangString("ExistOption");
                return false;
            }
            else
            {
                dal.Update(list[0]);
                dal.Update(list[1]);
                ValidMessage = GetLangString("SaveSuccessful");
                return true;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int OptionID)
        {
            ValidMessage = "";
            if (IsContainedInCategory(OptionID)) {
                ValidMessage = GetLangString("OptionContainedInCategory");
                return false;
            }
            else
            {
                ValidMessage = GetLangString("DeleteSuccessful");
                dal.Delete(OptionID);
                return true;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.POption GetModel(int OptionID, LanguageEnum LanguageID)
        {

            return dal.GetModel(OptionID, (int)LanguageID);
        }



        /// <summary>
        /// 根据属性英语名称获取属性ID(必须保证属性名称是唯一的)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int GetPOptionID(string name)
        {
            string sql = string.Format("SELECT OptionID  FROM  POption WHERE LanguageID=2 AND  OptionName='{0}'", name);
            object id = DbHelperSQL.GetSingle(sql);
            if (id == null)
                return 0;
            return Convert.ToInt16(id);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetPageList(int pagesize, int pageindex, string languageID, string fieldType, string optionName)
        {
            return dal.GetPageList(pagesize, pageindex, languageID, fieldType, optionName);
        }


        public long GetListCount(string strWhere)
        {
            return dal.GetListCount(strWhere);
        }

        public static bool IsContainedInCategory(int OptionID)
        {
            return DbHelperSQL.Exists("Select * from POptionRelCategory where OptionID=" + OptionID);
        }


        public DataSet GetSearchableListByCategory(int categoryID, int languageID,string where,ref int productCount,int brandId)
        {

            return dal.GetSearchableListByCategory(categoryID, languageID,where,ref productCount,brandId);
        }



   
        #endregion  成员方法
    }
}
